<template>
  <div class="login-container login-bg">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          <el-image style="width: 40px; height: 40px; margin-right: 10px;" :src="logo" />
          OTC ADMIN</h3>
      </div>

      <el-form-item prop="mobile">
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="请输入手机号"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code" class="recode-box">
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="请输入验证码"
          name="code"
          type="text"
          tabindex="3"
          auto-complete="on"
          style="width: 200px"
        />

        <el-image class="codeImg" :src="codeUrl" @click.native="getCode" />
        <!-- <img class="codeImg" :src="codeUrl" @click.native="getCode" /> -->
      </el-form-item>

      <!-- <div class="login-btn" @click.native.prevent="handleLogin">LOGIN</div> -->
      <el-form-item>
        <el-button class="login-btn" :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @keyup.enter.native="handleLogin" @click.native.prevent="handleLogin">Login</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>

export default {
  name: 'Login',
  data() {
    const validateMobile = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error('请填写手机号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请填写6位以上的密码'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('请填写验证码'))
      } else {
        callback()
      }
    }
    return {
      logo: require('../../assets/logo1.png'),
      codeUrl: '',
      loginForm: {
        mobile: '',
        password: '',
        code: ''
      },
      loginRules: {
        mobile: [{ required: true, trigger: 'blur', validator: validateMobile }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.keyupSubmit()
  },
  mounted() {
    this.getCode()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    keyupSubmit() {
      document.onkeydown = e => {
        const _key = window.event.keyCode
        if (_key === 13) {
          this.handleLogin()
        }
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', {
            mobile: this.loginForm.mobile,
            password: this.loginForm.password,
            verify_code: this.loginForm.code
          }).then((res) => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.getCode()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取验证码
    getCode() {
      this.$store.dispatch('user/captcha', { refresh: 1 }).then((res) => {
        this.codeUrl = process.env.VUE_APP_BASE_API + res.url
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import './style.scss';
</style>

<style lang="scss">
  .login-form {

.recode-box{
.el-form-item__content{
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .codeImg{
        height: 40px;
        width: 100px;
        background-color: turquoise;
        margin-left: 20px;
      }
    }

    .el-input {
      .el-input__inner{
          background-color: rgba(255,255,255,0);
          border: 1px solid #03e9f4;
          color: #fff;
          border-radius: 0;
      }
    }
  }
</style>
